package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeChangeProjectHistoryService;
import com.bidevalution.dao.entity.BeChangeProjectHistory;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.mapper.BeChangeProjectHistoryMapper;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.exception.BizException;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BeChangeProjectHistoryServiceImpl implements BeChangeProjectHistoryService {

	@Autowired(required = false)
	private BeSectionInfoMapper beSectionInfoMapper;
	@Autowired(required = false)
	private BeChangeProjectHistoryMapper beChangeProjectHistoryMapper;


	@Override
	public void insertChangeProjectId(String projectId) {
		if(OConvertUtils.isEmpty(projectId)){
			throw new BizException("项目id不可为空！");
		}
		BeChangeProjectHistory projectHistory = new BeChangeProjectHistory();
		projectHistory.setSeqId(IdsUtil.uuid());
		projectHistory.setProjectid(projectId);
		projectHistory.setChangeafterprojectid(IdsUtil.uuid());
		projectHistory.setState("0");
		projectHistory.setChangenum(0);
		projectHistory.setCreatetime(DateUtil.now());
		beChangeProjectHistoryMapper.insertSelective(projectHistory);
	}

	@Override
	public void insertChangeProjectIdBySectionId(String sectionId) {
		if(OConvertUtils.isEmpty(sectionId)){
			throw new BizException("标段id不可为空！");
		}
		BeSectionInfo sectionInfoById = beSectionInfoMapper.getSectionInfoById(sectionId);
		insertChangeProjectId(sectionInfoById.getTdProjectid());
	}

	@Override
	public List<BeChangeProjectHistory> getPendingList(Map<String, Object> params) {
		return beChangeProjectHistoryMapper.getPendingList(params);
	}

	@Override
	@Transactional
	public void executeDisposeChangeProjectId(BeChangeProjectHistory beChangeProjectHistory) {
		String projectid = beChangeProjectHistory.getProjectid();
		String changeafterprojectid = beChangeProjectHistory.getChangeafterprojectid();
		if (OConvertUtils.isNotEmpty(projectid) && OConvertUtils.isNotEmpty(changeafterprojectid)){
			beChangeProjectHistoryMapper.updateAllTableInProjectId(projectid,changeafterprojectid,DateUtil.getCurrentDateStr(DateUtil.PT5));
		}
		beChangeProjectHistory.setUpdatetime(DateUtil.now());
		beChangeProjectHistory.setState(Constants.CHANGE_PROJECT_ID_SUCCEED);
		beChangeProjectHistoryMapper.updateByPrimaryKeySelective(beChangeProjectHistory);
	}

	@Override
	public int updateByPrimaryKeySelective(BeChangeProjectHistory beChangeProjectHistory) {
		return beChangeProjectHistoryMapper.updateByPrimaryKeySelective(beChangeProjectHistory);
	}

}
